home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Foxpro 2.6 {Windows} / CPZERO.PR_ / CPZERO.bin
Text File  |  1994-03-10  |  4KB  |  167 lines

  1. * CPZERO -- Poke a codepage byte into a database header
  2. * Author: Walter Kennamer
  3. * Copyright Microsoft Corp, 1993
  4. *
  5. * Usage: 
  6. *    DO CPZERO WITH dbfname                     && marks the database with codepage 0 (i.e., no codepage)
  7. *    DO CPZERO WITH dbfname, codepage_number    && marks the database with specified codepage
  8. *
  9. * Some common valid numbers are:
  10. *   Windows            1252
  11. *   DOS                 437
  12. *   International DOS   850
  13. *
  14.  
  15. PARAMETER m.fname, m.cpbyte
  16. IF SET("TALK") = "ON"
  17.    SET TALK OFF
  18.    m.mtalk = "ON"
  19. ELSE
  20.    m.mtalk = "OFF"
  21. ENDIF   
  22.  
  23. #define C_TOTAL 20     && total code page numbers suppoted
  24.  
  25. IF PARAMETERS() < 2
  26.    m.cpbyte = 0
  27. ENDIF   
  28.  
  29. PRIVATE m.mtalk, m.vuename
  30.  
  31. #define c_buf_size 32
  32.  
  33. #define c_noopen   1
  34. #define c_badbyte  2
  35. #define c_notfox   3
  36. #define c_maxerror 4
  37.  
  38. m.vuename = ""
  39.  
  40. DECLARE error_array[c_maxerror]
  41. error_array[c_noopen] = "The database could not be opened."
  42. error_array[c_badbyte] = "Invalid code page specified."
  43. error_array[c_notfox] = "Not a FoxPro table."
  44.  
  45. DO setup
  46. DO main
  47. DO cleanup
  48.  
  49. PROCEDURE setup
  50. m.vuename = SYS(2023)+"\"+SYS(3)+".VUE"
  51. CREATE VIEW (m.vuename)
  52.  
  53.  
  54. PROCEDURE cleanup
  55. IF FILE(m.vuename)
  56.    SET VIEW TO (m.vuename)
  57.    DELETE FILE (m.vuename)
  58. ENDIF   
  59. SET TALK &mtalk
  60.  
  61. PROCEDURE main
  62. PRIVATE m.fp_in, m.buf, m.found_one, m.i, m.outbyte
  63.  
  64. * Set up table of code pages and DBF bytes numbers
  65. DIMENSION cpnums[C_TOTAL,2] 
  66. cpnums[ 1,1] = 437
  67. cpnums[ 1,2] = 1
  68. cpnums[ 2,1] = 850
  69. cpnums[ 2,2] = 2
  70. cpnums[ 3,1] = 1252
  71. cpnums[ 3,2] = 3
  72. cpnums[ 4,1] = 10000
  73. cpnums[ 4,2] = 4
  74. cpnums[ 5,1] = 852
  75. cpnums[ 5,2] = 100
  76. cpnums[ 6,1] = 866
  77. cpnums[ 6,2] = 101
  78. cpnums[ 7,1] = 865
  79. cpnums[ 7,2] = 102
  80. cpnums[ 8,1] = 861
  81. cpnums[ 8,2] = 103
  82. cpnums[ 9,1] = 895
  83. cpnums[ 9,2] = 104
  84. cpnums[10,1] = 620
  85. cpnums[10,2] = 105
  86. cpnums[11,1] = 737
  87. cpnums[11,2] = 106
  88. cpnums[12,1] = 857
  89. cpnums[12,2] = 107
  90. cpnums[13,1] = 10007
  91. cpnums[13,2] = 150
  92. cpnums[14,1] = 10029
  93. cpnums[14,2] = 151
  94. cpnums[15,1] = 10006
  95. cpnums[15,2] = 152
  96. cpnums[16,1] = 1250
  97. cpnums[16,2] = 200
  98. cpnums[17,1] = 1251
  99. cpnums[17,2] = 201
  100. cpnums[18,1] = 1253
  101. cpnums[18,2] = 203
  102. cpnums[19,1] = 1254
  103. cpnums[19,2] = 202
  104. cpnums[20,1] = 0
  105. cpnums[20,2] = 0
  106.  
  107. IF EMPTY(m.fname)
  108.    m.fname = getfile("DBF|SCX|FRX|LBX|MNX","DBF name")
  109. ENDIF
  110. IF !EMPTY(m.fname)
  111.    CLOSE DATABASES
  112.    m.outbyte = m.cpbyte
  113.    m.found_one = .F.
  114.    FOR m.i = 1 TO C_TOTAL
  115.       IF m.cpbyte = cpnums[m.i,1]
  116.          m.outbyte = cpnums[m.i,2]
  117.          m.found_one = .T.
  118.          EXIT
  119.       ENDIF
  120.    ENDFOR
  121.    IF m.found_one
  122.       m.cpbyte = m.outbyte
  123.    ELSE
  124.       * Was it a valid DBF byte if it wasn't a valid code page?
  125.       FOR m.i = 1 TO C_TOTAL
  126.          IF m.cpbyte = cpnums[m.i,2]
  127.             m.found_one = .T.
  128.          ENDIF
  129.       ENDFOR
  130.       IF !m.found_one
  131.          DO errormsg WITH c_badbyte
  132.          RETURN TO cpzero
  133.       ENDIF
  134.    ENDIF
  135.    
  136.    IF FILE(m.fname)
  137.        m.fp_in = FOPEN(m.fname,2)
  138.        IF m.fp_in > 0
  139.           * First check that we have a FoxPro table...
  140.           m.buf=FREAD(m.fp_in,c_buf_size)
  141.           IF (SUBSTR(m.buf,1,1) = CHR(139) OR SUBSTR(m.buf,1,1) = CHR(203);
  142.              OR SUBSTR(m.buf,31,1) != CHR(0) OR SUBSTR(m.buf,32,1) != CHR(0))
  143.               =fclose(m.fp_in)
  144.               DO errormsg WITH c_notfox
  145.               RETURN TO cpzero
  146.           ELSE
  147.               * Now poke the codepage id into byte 29
  148.               =FSEEK(m.fp_in,29)
  149.               =FWRITE(m.fp_in,CHR(m.cpbyte)) 
  150.               =FCLOSE(m.fp_in)
  151.           ENDIF
  152.        ELSE
  153.           DO errormsg WITH c_noopen
  154.           RETURN TO cpzero
  155.        ENDIF
  156.     ELSE
  157.        DO errormsg WITH c_noopen
  158.        RETURN TO cpzero
  159.     ENDIF
  160. ENDIF      
  161.  
  162.  
  163. PROCEDURE errormsg
  164. PARAMETER num
  165. WAIT WINDOW error_array[num] NOWAIT
  166.